/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package se.kth.lwr.jeat.samples.onsitetreatment;

import se.kth.lwr.jeat.core.Activity;
import se.kth.lwr.jeat.core.Inventory;

/**
 * A class that represent a treatment unit that handle wastewater. The idea with
 * this class is to separate the emission flows form wastewater from other
 * possible sources. In that way we can connect treatment units to each other
 * serially and let one unit process wastewater treated by another module. The
 * last step in the process is responsible for adding the resulting wastewater
 * inventory to the total inventory of the activity.
 *
 * @author davidev
 */
public abstract class WasteWaterTreatmentUnit extends Activity {

    // incoming wastewater
    private WasteWaterTreatmentUnit wastewaterInflow = null;
    // determines if the unit has pretreatment steps
    private boolean hasPreTreatmentUnit = false;
    /*
     * determins if the unit is the last in the treatment chain
     *
     */
    private boolean islast = true;

    /**
     * Creates a WastewaterTreatmentModule with a connected pretreatment unit
     *
     * @param name
     * @param description
     * @param preTreatmentStep
     */
    public WasteWaterTreatmentUnit(String name, String description, WasteWaterTreatmentUnit preTreatmentStep) {
        super(name, description);
        connectPreTreatmentUnit(preTreatmentStep);
    }

    /**
     * Creates a WastewaterTreatmentModule with no connected pretreatment unit.
     * By default this module will process raw wastewater. Pretreatment step can
     * be set afterwards through {@link connectPreTreatmentUnit(WasteWaterTreatmentUnit module}
     *
     * @param name
     * @param description
     */
    public WasteWaterTreatmentUnit(String name, String description) {
        super(name, description);


    }

    /**
     * Connects a preTreatmentModule to this module. If a connection is present
     * an UnsupportedOperationException will be thrown
     *
     * @param unit the pre treatment step
     */
    public void connectPreTreatmentUnit(WasteWaterTreatmentUnit unit) {
        if (!hasPreTreatmentUnit) {
            wastewaterInflow = unit;
            unit.setIsFinalUnit(false);
            hasPreTreatmentUnit = true;
        } else {
            //to avoid misstakes... through exception if conection allready established
            throw new UnsupportedOperationException("Pretreatment unit allready connected");
        }
    }

    /**
     * This function should process the wastewater that comes from pre treatment
     * steps. A wastewater inventory that comes from pretreatment steps are
     * passed as argument to this function
     *
     * @param inventory inventory of the incoming wastewater
     * @return an inventory of processed wastewater
     */
    protected abstract Inventory processWasteWaterInventory(Inventory inventory);

    /**
     * Returns an inventory of the wastewater that has been processed by this
     * treatment module
     *
     *
     * @return the resulting inventory
     */
    public Inventory getWasteWaterInventory() {
        //if no prestep connected pass raw wastewater to process function
        if (!hasPreTreatmentUnit) {
            return processWasteWaterInventory(GlobalData.rawWasteWater.useValue(this).getInventory());
        } else {
            //otherwise pass the treated wastewater
            return processWasteWaterInventory(wastewaterInflow.getWasteWaterInventory());
        }
    }
    
    /**
     * 
     * @return true if this unit has no subsequent treatment unit connected
     */
    public boolean isFinalUnit() {
        return islast;
    }
    
    /**
     * Sets if this unit is the final treatment step or not
     * @param isfinal 
     */
    protected void setIsFinalUnit(boolean isfinal) {
        islast = isfinal;
    }
}
